package com.aya.activity.core.composite;

import com.aya.activity.core.model.Goods;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class GoodsGroup<T> implements AbstractGoodsGroup<T> {


    Map<T,Goods> goodsMap = new HashMap<>();


    @Override
    public <T> T getId() {
        return null;
    }

    @Override
    public BigDecimal getPrice() {
        Optional<BigDecimal> reduce = goodsMap.values().stream().map(Goods::getPrice).reduce(BigDecimal::add);
        if(!reduce.isPresent()){
            throw new RuntimeException("获得价格失败");
        }
        return reduce.get();
    }

    @Override
    public AbstractGoodsGroup<T> add(T id, Goods goods) {
        goodsMap.put(id,goods);
        return this;
    }

    @Override
    public void delete(T id) {
        goodsMap.remove(id);
    }
}
